package com.zheng.api.server.service.task;

import com.alibaba.fastjson.JSONObject;
import com.zheng.api.common.constant.ApiBaseResult;
import com.zheng.api.common.constant.ApiConstant;
import com.zheng.api.common.constant.ApiResult;
import com.zheng.api.common.constant.ApiResultConstant;
import com.zheng.api.server.service.ApiServerUmbrellaService;
import com.zheng.api.server.service.mq.um.Mq4UmbrellaMachineNotify;
import com.zheng.common.base.BaseResult;
import com.zheng.common.util.BizSequenceUtils;
import com.zheng.common.util.DateUtil;
import com.zheng.common.util.JsonUtil;
import com.zheng.common.util.ModuleConstant;
import com.zheng.umbrella.dao.model.UmbrellaHardwareMachine;
import com.zheng.upms.rpc.api.UpmsSystemParamService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MachineStatusReportTaskService {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(MachineStatusReportTaskService.class);

    @Autowired
    UpmsSystemParamService upmsSystemParamService;
    @Autowired
    ApiServerUmbrellaService apiServerUmbrellaService;
    @Autowired
    Mq4UmbrellaMachineNotify mq4UmbrellaMachineNotify;

    //任务逻辑执行体
    public ApiBaseResult machineStatusReportNotify(){
        LOGGER.debug("machineStatusReportNotify begin：{}", DateUtil.getDate());
        try {
            boolean reprotSwitch = Boolean.valueOf(upmsSystemParamService.querySystemParamValueByParam_key("UMBRELLA_MACHINE_STATUSREPORT_SWITCH"));
            LOGGER.info("machineStatusReportNotify reprotSwitch：{}", reprotSwitch);
            if (reprotSwitch) {
            /*
                {"command":"10002","machineCode":"dev0001","code":1,"message":"","data":{"serialNo":"20180929100020001",
                "dataPackage":{"account":10000004,"token":"SDKJLJBB_JLKSDJFLKDSJFDSJFLJ-IJOIUEWRPOI"}}}
             */

                ArrayList<Integer> statusList = new ArrayList<Integer>();
                statusList.add(ApiConstant.UM_MACHINE_STATUS_UP);
                List<UmbrellaHardwareMachine> machineList = apiServerUmbrellaService.baseSelectUmbrellaHardwareMachineListByStatus(statusList);
                for (int i = 0; i < machineList.size(); i++) {
                    String machineCode = machineList.get(i).getCode();
                    //业务流水号
                    String bizSeqNo = BizSequenceUtils.getInstance().generateBizSeqNo(ModuleConstant.UMBRELLA_COMMON_BIZ_SEQUENCE_NO_PREFIX);
                    JSONObject machineDataInfo = new JSONObject();
                    JSONObject dataPackage = new JSONObject();
                    dataPackage.put("account", 10000004);
                    dataPackage.put("token", "SDKJLJBB_JLKSDJFLKDSJFDSJFLJ-IJOIUEWRPOI");
                    machineDataInfo.put("serialNo", bizSeqNo);
                    machineDataInfo.put("dataPackage", dataPackage);
                    ApiResult machineNotifyInfo =
                            new ApiResult(ApiResultConstant.SUCCESS.code, ApiResultConstant.SUCCESS.message, ApiConstant.UMBRELLA_MACHINE_COMMAND_10002, machineCode, machineDataInfo);
                    //machineNotifyInfo.setMachineCode(machineCode);
                    mq4UmbrellaMachineNotify.send(JsonUtil.object2Json(machineNotifyInfo), machineCode);
                }
            }
        }catch (Exception e){
            LOGGER.error("MachineStatusReportTask error",e);
        }
        LOGGER.debug("machineStatusReportNotify end：{}", DateUtil.getDate());
        return new ApiBaseResult(ApiResultConstant.SUCCESS,null);
    }
}
